const size = 1024 * 1024;

function upload(start) {
    let fileObj = $("#course-resource-file")[0].files[0];
    if (start >= fileObj.size) {
        $('#fileSize').text('上传完成').css('color', 'green');
        $('#file').val(null);
        $("#progress-border").css("display", "none");
        return;
    }

    //获取文件块的终止字节
    let end = (start + size > fileObj.size) ? fileObj.size : (start + size);
    //将文件切块上传
    let fd = new FormData();
    fd.append("start", start);
    fd.append('f', fileObj.slice(start, end));
    fd.append('filename', fileObj.name);
    //post表单提交
    $.ajax({
        url: '${pageContext.request.contextPath}/resource/upload',
        type: 'post',
        data: fd,
        processData: false,
        contentType: false,
        success: function () {
            $('#fileSize').text(start + '/' + fileObj.size);
            $("#progress").css("width", start * 200 / fileObj.size + "px");
            upload(end);
        }
    });
}


function init() {
    let fileObj = $("#course-resource-file")[0].files[0];
    $("#progress-border").css("display", "block");

    //获取文件已经上传的大小
    $.get(
        '${pageContext.request.contextPath}/resource/getSize',
        {fileName: fileObj.name},
        function (data) {
            let start = parseInt(data);
            alert(start);
            $('#fileSize').text(start + '/' + fileObj.size).css('color', 'blue');

            //开始上传
            upload(start);
            if (start == fileObj.size) {
                alert("秒传");
                $("#progress-border").css("display", "none");
            }
        }
    );
}

//上传进度条JS
function showWaiting() {
    $('#waitingModal').modal({
        keyboard: false,
        backdrop: 'static',
        show: true
    });
}

function hideWaiting() {
    $('#waitingModal').modal('hide');
}